<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Windchill拉模在线建模</title>
    <script src="/static/js/tailwindcss-3.4.16.js"></script>
    <!-- <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet"> -->
    <link href="/static/css/font-awesome-4.7.0.min.css" rel="stylesheet">
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#165DFF',
                        secondary: '#0FC6C2',
                        neutral: '#F5F7FA',
                        success: '#00B42A',
                        warning: '#FF7D00',
                        danger: '#F53F3F',
                    },
                    fontFamily: {
                        inter: ['Inter', 'system-ui', 'sans-serif'],
                    },
                }
            }
        }
    </script>
    <style type="text/tailwindcss">
        @layer utilities {
            .content-auto {
                content-visibility: auto;
            }
            .scrollbar-hide::-webkit-scrollbar {
                display: none;
            }
            .scrollbar-hide {
                -ms-overflow-style: none;
                scrollbar-width: none;
            }
            .text-shadow {
                text-shadow: 0 2px 4px rgba(0,0,0,0.1);
            }
            .animate-pulse-slow {
                animation: pulse 3s cubic-bezier(0.4, 0, 0.6, 1) infinite;
            }
        }
    </style>
</head>

<body class="font-inter bg-gray-50 h-screen flex flex-col"> <!-- 修改此处，使用 h-screen 替代 min-h-screen -->
    <!-- 导航栏 -->
    <nav class="bg-white shadow-md fixed w-full z-50 transition-all duration-300" id="navbar">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="flex justify-between h-16">
                <div class="flex items-center">
                    <i class="fa fa-cubes text-primary text-2xl mr-3"></i>
                    <span class="text-xl font-bold text-gray-800">Windchill拉模在线建模</span>
                </div>
                <div class="flex items-center space-x-4">
                    <span class="text-sm text-gray-600" id="connection-status">
                        <i class="fa fa-circle-o text-gray-400 mr-1"></i>未连接
                    </span>
                </div>
            </div>
        </div>
    </nav>

    <!-- 主内容区 -->
    <!-- 修改为 max-w-none 调宽主内容区 -->
    <main class="flex-grow pt-20 pb-2 px-4 sm:px-6 lg:px-8 max-w-none mx-auto w-full">
        <!-- 状态卡片 -->
        <div class="grid grid-cols-1 md:grid-cols-3 gap-4 mb-8">
            <div class="bg-white rounded-xl shadow-md p-6 transform transition-all duration-300 hover:shadow-lg">
                <div class="flex items-center">
                    <div class="p-3 bg-primary/10 rounded-lg mr-4">
                        <i class="fa fa-upload text-primary text-xl"></i>
                    </div>
                    <div>
                        <h3 class="text-sm font-medium text-gray-500">已上传文件</h3>
                        <p class="text-2xl font-bold text-gray-800" id="upload-count">0</p>
                    </div>
                </div>
            </div>
            <div class="bg-white rounded-xl shadow-md p-6 transform transition-all duration-300 hover:shadow-lg">
                <div class="flex items-center">
                    <div class="p-3 bg-secondary/10 rounded-lg mr-4">
                        <i class="fa fa-cogs text-secondary text-xl"></i>
                    </div>
                    <div>
                        <h3 class="text-sm font-medium text-gray-500">建模状态</h3>
                        <p class="text-2xl font-bold text-gray-800" id="modeling-status">就绪</p>
                    </div>
                </div>
            </div>
            <div class="bg-white rounded-xl shadow-md p-6 transform transition-all duration-300 hover:shadow-lg">
                <div class="flex items-center">
                    <div class="p-3 bg-success/10 rounded-lg mr-4">
                        <i class="fa fa-clock-o text-success text-xl"></i>
                    </div>
                    <div>
                        <h3 class="text-sm font-medium text-gray-500">总耗时</h3>
                        <p class="text-2xl font-bold text-gray-800" id="total-time">00:00:00</p>
                    </div>
                </div>
            </div>
        </div>

        <div class="grid grid-cols-1 lg:grid-cols-4 gap-8">
            <!-- 文件上传区 -->
            <div class="lg:col-span-1 space-y-6">
                <div class="bg-white rounded-xl shadow-md p-6 transform transition-all duration-300 hover:shadow-lg">
                    <h2 class="text-lg font-semibold text-gray-800 mb-4 flex items-center">
                        <i class="fa fa-file-text-o text-primary mr-2"></i>文件上传
                    </h2>
                    <div class="space-y-4">
                        <div id="drop-area"
                            class="border-2 border-dashed border-gray-300 rounded-lg p-6 text-center transition-all duration-300 hover:border-primary">
                            <i class="fa fa-cloud-upload text-4xl text-gray-400 mb-2"></i>
                            <p class="text-gray-500 mb-2">拖放文件到此处上传</p>
                            <p class="text-xs text-gray-400 mb-4">或</p>
                            <label
                                class="bg-primary hover:bg-primary/90 text-white py-2 px-4 rounded-lg cursor-pointer transition-all duration-300 transform hover:scale-105 inline-flex items-center">
                                <i class="fa fa-plus mr-2"></i>选择文件
                                <input type="file" id="file-input" class="hidden" multiple>
                            </label>
                        </div>

                        <div id="file-list" class="space-y-2 max-h-60 overflow-y-auto scrollbar-hide">
                            <!-- 文件列表将动态生成 -->
                            <div class="text-center text-gray-500 py-4">
                                暂无上传文件
                            </div>
                        </div>

                        <button id="start-modeling"
                            class="w-full bg-primary hover:bg-primary/90 text-white font-medium py-3 px-4 rounded-lg transition-all duration-300 transform hover:scale-[1.02] flex items-center justify-center disabled:opacity-50 disabled:cursor-not-allowed"
                            disabled>
                            <i class="fa fa-play mr-2"></i>开始建模
                        </button>
                    </div>
                </div>

                <div class="bg-white rounded-xl shadow-md p-6 transform transition-all duration-300 hover:shadow-lg">
                    <h2 class="text-lg font-semibold text-gray-800 mb-4 flex items-center">
                        <i class="fa fa-download text-primary mr-2"></i>结果下载
                    </h2>
                    <div class="space-y-4">
                        <div class="text-gray-600 mb-2">
                            建模完成后，点击下方按钮下载结果文件
                        </div>
                        <button id="download-results"
                            class="w-full bg-success hover:bg-success/90 text-white font-medium py-3 px-4 rounded-lg transition-all duration-300 transform hover:scale-[1.02] flex items-center justify-center disabled:opacity-50 disabled:cursor-not-allowed"
                            disabled>
                            <i class="fa fa-download mr-2"></i>下载结果文件
                        </button>
                    </div>
                </div>
            </div>

            <!-- 日志显示区 -->
            <!-- 修改高度相关样式，让其自适应 -->
            <div
                class="lg:col-span-3 bg-white rounded-xl shadow-md p-6 flex flex-col transform transition-all duration-300 hover:shadow-lg">
                <div class="flex justify-between items-center mb-4">
                    <h2 class="text-lg font-semibold text-gray-800 flex items-center">
                        <i class="fa fa-terminal text-primary mr-2"></i>建模日志
                    </h2>
                    <div class="flex space-x-2">
                        <button id="clear-log"
                            class="text-gray-500 hover:text-gray-700 transition-colors duration-300 p-1.5 rounded-full hover:bg-gray-100">
                            <i class="fa fa-trash-o"></i>
                        </button>
                        <button id="copy-log"
                            class="text-gray-500 hover:text-gray-700 transition-colors duration-300 p-1.5 rounded-full hover:bg-gray-100">
                            <i class="fa fa-copy"></i>
                        </button>
                        <button id="auto-scroll"
                            class="text-gray-500 hover:text-gray-700 transition-colors duration-300 p-1.5 rounded-full hover:bg-gray-100">
                            <i class="fa fa-arrow-down"></i>
                        </button>
                    </div>
                </div>

                <!-- 日志容器保持不自动换行且支持左右上下滚动，添加 h-[400px] 固定高度 -->
                <div id="log-container"
                    class="flex-grow bg-gray-50 rounded-lg p-4 overflow-auto border border-gray-200 whitespace-nowrap h-[400px]">
                    <!-- 移除 scrollbar-hide 类 -->
                    <div id="log-messages" class="space-y-2 text-sm">
                        <div class="text-gray-500">
                            <i class="fa fa-info-circle mr-1"></i>欢迎使用Windchill拉模在线建模系统
                        </div>
                        <div class="text-gray-500">
                            <i class="fa fa-info-circle mr-1"></i>请上传需要处理的文件，然后点击"开始建模"按钮
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </main>

    <!-- 页脚 -->
    <footer class="bg-white border-t border-gray-200 py-4">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="text-gray-500 text-sm mb-4 md:mb-0">
                    © 2025 Windchill拉模在线建模系统 - 保留所有权利
                </div>
                <div class="flex space-x-6">
                    <a href="#" class="text-gray-400 hover:text-primary transition-colors duration-300">
                        <i class="fa fa-question-circle"></i>
                        <span class="ml-1">帮助</span>
                    </a>
                    <a href="#" class="text-gray-400 hover:text-primary transition-colors duration-300">
                        <i class="fa fa-book"></i>
                        <span class="ml-1">文档</span>
                    </a>
                    <a href="#" class="text-gray-400 hover:text-primary transition-colors duration-300">
                        <i class="fa fa-envelope-o"></i>
                        <span class="ml-1">联系我们</span>
                    </a>
                </div>
            </div>
        </div>
    </footer>

    <!-- 通知组件 -->
    <div id="notification-container" class="fixed top-20 right-4 z-50 flex flex-col space-y-3 max-w-xs"></div>

    <script>
        // 全局变量
        let files = [];
        let startTime = null;
        let timerInterval = null;
        let autoScrollEnabled = true;
        let socket = null;

        let clientId = '';
        let taskId = '';

        // DOM元素
        const dropArea = document.getElementById('drop-area');
        const fileInput = document.getElementById('file-input');
        const fileList = document.getElementById('file-list');
        const startModelingBtn = document.getElementById('start-modeling');
        const downloadResultsBtn = document.getElementById('download-results');
        const logMessages = document.getElementById('log-messages');
        const logContainer = document.getElementById('log-container');
        const clearLogBtn = document.getElementById('clear-log');
        const copyLogBtn = document.getElementById('copy-log');
        const autoScrollBtn = document.getElementById('auto-scroll');
        const uploadCount = document.getElementById('upload-count');
        const modelingStatus = document.getElementById('modeling-status');
        const totalTime = document.getElementById('total-time');
        const connectionStatus = document.getElementById('connection-status');
        const notificationContainer = document.getElementById('notification-container');
        const navbar = document.getElementById('navbar');

        // 初始化
        init();

        function init() {
            // 设置拖拽上传
            setupDragDrop();

            // 设置文件选择
            fileInput.addEventListener('change', handleFileSelect);

            // 设置开始建模按钮
            startModelingBtn.addEventListener('click', createTask);

            // 设置下载结果按钮
            downloadResultsBtn.addEventListener('click', downloadResults);

            // 设置清除日志按钮
            clearLogBtn.addEventListener('click', clearLog);

            // 设置复制日志按钮
            copyLogBtn.addEventListener('click', copyLog);

            // 设置自动滚动按钮
            autoScrollBtn.addEventListener('click', toggleAutoScroll);

            // 设置滚动事件监听
            logContainer.addEventListener('scroll', handleScroll);

            // 初始化WebSocket连接
            initWebSocket();

            // 设置导航栏滚动效果
            window.addEventListener('scroll', () => {
                if (window.scrollY > 10) {
                    navbar.classList.add('shadow-lg');
                    navbar.classList.remove('shadow-md');
                } else {
                    navbar.classList.remove('shadow-lg');
                    navbar.classList.add('shadow-md');
                }
            });

            // 更新UI状态
            updateUI();
        }

        // 设置拖拽上传
        function setupDragDrop() {
            ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
                dropArea.addEventListener(eventName, preventDefaults, false);
            });

            function preventDefaults(e) {
                e.preventDefault();
                e.stopPropagation();
            }

            ['dragenter', 'dragover'].forEach(eventName => {
                dropArea.addEventListener(eventName, highlight, false);
            });

            ['dragleave', 'drop'].forEach(eventName => {
                dropArea.addEventListener(eventName, unhighlight, false);
            });

            function highlight() {
                dropArea.classList.add('border-primary', 'bg-primary/5');
            }

            function unhighlight() {
                dropArea.classList.remove('border-primary', 'bg-primary/5');
            }

            dropArea.addEventListener('drop', handleDrop, false);

            function handleDrop(e) {
                const dt = e.dataTransfer;
                const droppedFiles = Array.from(dt.files);
                addFiles(droppedFiles);
            }
        }

        // 处理文件选择
        function handleFileSelect(e) {
            const selectedFiles = Array.from(e.target.files);
            addFiles(selectedFiles);
        }

        // 添加文件到列表
        function addFiles(newFiles) {
            newFiles.forEach(file => {
                // 检查文件是否已存在
                if (!files.some(f => f.name === file.name && f.size === file.size && f.lastModified === file.lastModified)) {
                    files.push(file);
                    addFileToUI(file);
                }
            });

            updateUI();
        }

        // 将文件添加到UI
        function addFileToUI(file) {
            // 如果是第一个文件，清空"暂无上传文件"提示
            if (files.length === 1) {
                fileList.innerHTML = '';
            }

            const fileItem = document.createElement('div');
            fileItem.className = 'flex items-center justify-between bg-gray-50 p-3 rounded-lg animate-fade-in';
            fileItem.setAttribute('data-file-name', file.name);

            const fileInfo = document.createElement('div');
            fileInfo.className = 'flex items-center';

            const fileIcon = document.createElement('i');
            fileIcon.className = 'fa fa-file-o text-gray-400 mr-3';

            const fileName = document.createElement('span');
            fileName.className = 'text-gray-700 truncate max-w-[250px]';
            fileName.textContent = file.name;

            const fileSize = document.createElement('span');
            fileSize.className = 'text-xs text-gray-500 ml-2';
            fileSize.textContent = formatFileSize(file.size);

            const removeBtn = document.createElement('button');
            removeBtn.className = 'text-gray-400 hover:text-danger transition-colors duration-300 p-1.5 rounded-full hover:bg-gray-100';
            removeBtn.innerHTML = '<i class="fa fa-times"></i>';
            removeBtn.addEventListener('click', () => removeFile(file.name));

            fileInfo.appendChild(fileIcon);
            fileInfo.appendChild(fileName);
            fileInfo.appendChild(fileSize);

            fileItem.appendChild(fileInfo);
            fileItem.appendChild(removeBtn);

            fileList.appendChild(fileItem);
        }

        // 移除文件
        function removeFile(fileName) {
            files = files.filter(file => file.name !== fileName);

            const fileItem = fileList.querySelector(`[data-file-name="${fileName}"]`);
            if (fileItem) {
                fileItem.classList.add('opacity-0');
                setTimeout(() => {
                    fileItem.remove();
                    if (files.length === 0) {
                        fileList.innerHTML = `
                            <div class="text-center text-gray-500 py-4">
                                暂无上传文件
                            </div>
                        `;
                    }
                }, 300);
            }

            updateUI();
        }

        // 更新UI状态
        function updateUI() {
            // 更新上传文件计数
            uploadCount.textContent = files.length;

            // 启用/禁用开始建模按钮
            startModelingBtn.disabled = files.length === 0;

            // 更新建模状态
            if (timerInterval) {
                modelingStatus.textContent = '运行中';
                modelingStatus.className = 'text-2xl font-bold text-warning';
            } else {
                if (files.length === 0) {
                    modelingStatus.textContent = '就绪';
                    modelingStatus.className = 'text-2xl font-bold text-gray-800';
                } else {
                    modelingStatus.textContent = '准备就绪';
                    modelingStatus.className = 'text-2xl font-bold text-success';
                }
            }
        }

        // 格式化文件大小
        function formatFileSize(bytes) {
            if (bytes === 0) return '0 Bytes';

            const k = 1024;
            const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
            const i = Math.floor(Math.log(bytes) / Math.log(k));

            return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
        }

        // 开始建模
        function startModeling() {
            if (files.length === 0) return;

            // 禁用开始按钮
            startModelingBtn.disabled = true;

            // 启用下载按钮（初始状态，成功后会保持启用）
            downloadResultsBtn.disabled = false;

            // 创建表单数据
            const formData = new FormData();
            files.forEach(file => {
                formData.append('files', file);
            });
            formData.append('task_id', taskId);
            formData.append('client_id', clientId);

            // 显示开始消息
            addLogEntry('开始上传文件...', 'info');

            // 记录开始时间
            startTime = new Date();
            startTimer();

            // 发送文件到服务器
            fetch('/upload', {
                method: 'POST',
                body: formData
            })
                .then(response => {
                    if (!response.ok) {
                        throw new Error(`HTTP error! status: ${response.status}`);
                    }
                    addLogEntry('文件上传完成，开始建模...', 'info');
                    return response.json();
                })
                .then(data => {
                    // 建模已开始，WebSocket将接收实时日志
                    addLogEntry(`发送建模请求 (ID: ${taskId})`, 'info');
                    socket.send(JSON.stringify({
                        action: 'modeling',
                        task_id: taskId
                    }));
                })
                .catch(error => {
                    addLogEntry(`上传失败: ${error.message}`, 'error');
                    stopTimer();
                    downloadResultsBtn.disabled = false;
                    startModelingBtn.disabled = false;
                });
        }

        function createTask() {
            if (!clientId) {
                addLogEntry('客户端初始化未完成，请稍等或者刷新网页', 'info');
                return;
            }

            // 重置日志
            clearLog();

            //创建任务
            addLogEntry('获取任务id...', 'info');
            fetch('/create_task').then(response => {
                return response.text();  // 返回 Promise
            }).then(id => {
                taskId = id;
                addLogEntry('获取到的任务id ' + taskId, 'info');
                startModeling();
            });
        }

        // 下载结果
        function downloadResults() {
            // 创建一个临时的iframe来处理下载
            const iframe = document.createElement('iframe');
            iframe.style.display = 'none';
            iframe.src = '/download?task_id=' + taskId;
            document.body.appendChild(iframe);

            // 一段时间后移除iframe
            setTimeout(() => {
                document.body.removeChild(iframe);
            }, 3000);

            addLogEntry('开始下载结果文件...', 'info');
        }

        // 添加日志条目
        function addLogEntry(message, type = 'info') {
            const logEntry = document.createElement('div');
            let icon, className;

            switch (type) {
                case 'info':
                    icon = 'fa-info-circle';
                    className = 'text-gray-700';
                    break;
                case 'success':
                    icon = 'fa-check-circle';
                    className = 'text-success';
                    break;
                case 'warning':
                    icon = 'fa-exclamation-triangle';
                    className = 'text-warning';
                    break;
                case 'error':
                    icon = 'fa-times-circle';
                    className = 'text-danger';
                    break;
                default:
                    icon = 'fa-info-circle';
                    className = 'text-gray-700';
            }

            const timestamp = new Date().toLocaleTimeString();

            logEntry.className = `flex items-start ${className} opacity-0 transform translate-y-2 transition-all duration-300`;
            logEntry.innerHTML = `
                <i class="fa ${icon} mr-2 mt-0.5"></i>
                <div>
                    <span class="text-xs text-gray-500 mr-2">${timestamp}</span>
                    <span>${message}</span>
                </div>
            `;

            logMessages.appendChild(logEntry);

            // 触发动画
            setTimeout(() => {
                logEntry.classList.remove('opacity-0', 'translate-y-2');
            }, 10);

            // 自动滚动到底部
            if (autoScrollEnabled) {
                scrollToBottom();
            }
        }

        // 清除日志
        function clearLog() {
            logMessages.innerHTML = '';
        }

        // 复制日志
        function copyLog() {
            const logText = Array.from(logMessages.querySelectorAll('div'))
                .map(el => el.textContent)
                .join('\n');

            navigator.clipboard.writeText(logText)
                .then(() => {
                    showNotification('日志已复制到剪贴板', 'success');
                })
                .catch(err => {
                    showNotification('复制失败，请手动复制', 'error');
                });
        }

        // 切换自动滚动
        function toggleAutoScroll() {
            autoScrollEnabled = !autoScrollEnabled;

            if (autoScrollEnabled) {
                autoScrollBtn.innerHTML = '<i class="fa fa-arrow-down"></i>';
                autoScrollBtn.classList.remove('text-primary');
                scrollToBottom();
            } else {
                autoScrollBtn.innerHTML = '<i class="fa fa-lock"></i>';
                autoScrollBtn.classList.add('text-primary');
            }
        }

        // 处理滚动事件
        function handleScroll() {
            // 如果用户滚动到接近底部，重新启用自动滚动
            if (logContainer.scrollTop + logContainer.clientHeight >= logContainer.scrollHeight - 50) {
                autoScrollEnabled = true;
                autoScrollBtn.innerHTML = '<i class="fa fa-arrow-down"></i>';
                autoScrollBtn.classList.remove('text-primary');
            }
        }

        // 滚动到底部
        function scrollToBottom() {
            logContainer.scrollTop = logContainer.scrollHeight;
        }

        // 开始计时器
        function startTimer() {
            stopTimer();

            timerInterval = setInterval(() => {
                const now = new Date();
                const elapsedTime = new Date(now - startTime);

                const hours = elapsedTime.getUTCHours().toString().padStart(2, '0');
                const minutes = elapsedTime.getUTCMinutes().toString().padStart(2, '0');
                const seconds = elapsedTime.getUTCSeconds().toString().padStart(2, '0');

                totalTime.textContent = `${hours}:${minutes}:${seconds}`;
            }, 1000);
        }

        // 停止计时器
        function stopTimer() {
            if (timerInterval) {
                clearInterval(timerInterval);
                timerInterval = null;
            }
        }

        // 初始化WebSocket连接
        function initWebSocket() {
            // 创建WebSocket连接
            socket = new WebSocket(`ws://${window.location.host}/ws`);

            // 连接成功
            socket.onopen = () => {
                connectionStatus.innerHTML = '<i class="fa fa-circle text-success mr-1"></i>已连接';
                connectionStatus.classList.remove('text-gray-600');
                connectionStatus.classList.add('text-success');
                addLogEntry('WebSocket连接已建立，等待指令', 'info');
            };

            // 接收到消息
            socket.onmessage = (event) => {
                const data = JSON.parse(event.data);

                if (data.type === 'clientId') {
                    clientId = data.clientId;
                    addLogEntry('获取到客户端id:' + clientId, 'info');
                    return;
                }

                if (data.type === 'log') {
                    // 添加日志条目
                    addLogEntry(data.message, data.level);

                    // 如果是结束消息，更新状态
                    if (data.level === 'success' || data.level === 'error') {
                        stopTimer();
                        startModelingBtn.disabled = false;
                    }
                } else if (data.type === 'status') {
                    // 更新状态信息
                    if (data.status === 'started') {
                        startTimer();
                    } else if (data.status === 'finished') {
                        stopTimer();
                        downloadResultsBtn.disabled = false;
                        // 禁用开始按钮
                        startModelingBtn.disabled = false;
                    }
                }
            };

            // 连接关闭
            socket.onclose = () => {
                connectionStatus.innerHTML = '<i class="fa fa-circle-o text-gray-400 mr-1"></i>未连接';
                connectionStatus.classList.remove('text-success');
                connectionStatus.classList.add('text-gray-600');
                addLogEntry('WebSocket连接已断开', 'warning');

                // 尝试重新连接
                setTimeout(() => {
                    initWebSocket();
                }, 3000);
            };

            // 连接错误
            socket.onerror = (error) => {
                addLogEntry(`WebSocket错误: ${error.message}`, 'error');
            };
        }

        // 显示通知
        function showNotification(message, type = 'info') {
            const notification = document.createElement('div');
            notification.className = `bg-white rounded-lg shadow-lg p-4 transform transition-all duration-300 translate-x-full opacity-0 flex items-center`;

            let icon, bgColor;

            switch (type) {
                case 'success':
                    icon = 'fa-check-circle';
                    bgColor = 'bg-success/10 border-l-4 border-success';
                    break;
                case 'error':
                    icon = 'fa-times-circle';
                    bgColor = 'bg-danger/10 border-l-4 border-danger';
                    break;
                case 'warning':
                    icon = 'fa-exclamation-triangle';
                    bgColor = 'bg-warning/10 border-l-4 border-warning';
                    break;
                case 'info':
                default:
                    icon = 'fa-info-circle';
                    bgColor = 'bg-primary/10 border-l-4 border-primary';
            }

            notification.classList.add(...bgColor.split(' '));

            notification.innerHTML = `
                <i class="fa ${icon} text-xl mr-3 ${type === 'success' ? 'text-success' : type === 'error' ? 'text-danger' : type === 'warning' ? 'text-warning' : 'text-primary'}"></i>
                <div class="flex-grow">
                    <p class="text-sm font-medium">${message}</p>
                </div>
                <button class="text-gray-400 hover:text-gray-600 transition-colors duration-300">
                    <i class="fa fa-times"></i>
                </button>
            `;

            // 添加到容器
            notificationContainer.prepend(notification);

            // 显示动画
            setTimeout(() => {
                notification.classList.remove('translate-x-full', 'opacity-0');
            }, 10);

            // 关闭按钮
            const closeBtn = notification.querySelector('button');
            closeBtn.addEventListener('click', () => {
                notification.classList.add('translate-x-full', 'opacity-0');
                setTimeout(() => {
                    notification.remove();
                }, 300);
            });

            // 自动关闭
            setTimeout(() => {
                notification.classList.add('translate-x-full', 'opacity-0');
                setTimeout(() => {
                    notification.remove();
                }, 300);
            }, 5000);
        }
    </script>
</body>

</html>